﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using Dude.MLS;
using System.Text.RegularExpressions;

namespace Dude.Extensions
{
    public static class Extensions
    {
        /// <summary>
        /// Returns true if string is null or equals String.Empty
        /// </summary>
        public static bool IsNullOrEmpty(this string s)
        {
            return (s == null || s == String.Empty);
        }

        /// <summary>
        /// Returns true if string is null or string.Trim() equals String.Empty
        /// </summary>
        public static bool IsNullOrTrimEmpty(this string s)
        {
            return (s == null || s.Trim() == String.Empty);
        }

        /// <summary>
        /// Email address validation througt regular expression
        /// </summary>
        /// <returns>true if string represents valid email address</returns>
        public static bool IsValidEmailAddress(this string s)
        {
            Regex regex = new
              Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
            return regex.IsMatch(s);
        }

        /// <summary>
        /// Rename file to GetBackupName()
        /// </summary>
        /// <param name="fileinfo"></param>
        public static void MoveToBackup(this FileInfo fileinfo)
        {
            fileinfo.MoveTo(GetBackupName(fileinfo));
        }

        /// <summary>
        /// Get name of backup file 
        /// </summary>
        /// <returns></returns>
        private static string GetBackupName(this FileInfo fileinfo)
        {
            string b_extension = fileinfo.LastWriteTime.ToString("yyyyMMdd") + "-" + DateTime.Now.ToString("yyyyMMdd'-'HHmmss'-'fffffff");
            return fileinfo.FullName + "." + b_extension;
        }

        /// <summary>
        /// Raise message box, if file exists and returns true if file name should be used.
        /// 
        ///  * If file not exists, returns true.
        ///  * If user say yes, file is renamed. Returns true.
        ///  * If user say no, no action. Returns true.
        ///  * If user say cancel, no action. Returns false.
        /// </summary>
        /// <returns>true if file name can be used</returns>
        public static bool AskForBackup(this FileInfo fileinfo)
        {
            return fileinfo.AskForBackup(null);
        }

        /// <param name="window">IWin32Window for MessageBox.Show...</param>
        public static bool AskForBackup(this FileInfo fileinfo, IWin32Window window)
        {
            if (fileinfo.Exists)
            {
                string b_extension = fileinfo.LastWriteTime.ToString("yyyyMMdd") + "-" + DateTime.Now.ToString("yyyyMMdd'-'HHmmss'-'fffffff");
                switch (MessageBox.Show(window,
                    "File {0} exists.\nDo you want to create backup before overwrite it?".GetText(fileinfo.FullName, b_extension),
                    "Create backup?".GetText(), MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation))
                {
                    case DialogResult.Yes:
                        fileinfo.MoveToBackup();
                        return true;
                    case DialogResult.No:
                        return true;
                    case DialogResult.Cancel:
                        return false;
                }
            }
            return true;
        }
    
    }
}
